home *** CD-ROM | disk | FTP | other *** search
- #include "PICS_Operations.h"
- #include "PICS_Split.h"
- #include "PICS_Types.h" // Creator, file types
- #include "PICS_Utils.h"
-
- #include "FileRegistry.h"
- #include "Class_ProgressWindow.h"
- #include "Monitors.h"
- #include "SavePicture.h" // Save a PICT resource as a PICT file
- #include "StringUtils.h" // Append, manipulate filenames
-
- #include "main.h" // For AppEmergencyUpdate()
-
- #include "SimpleError.h" // For error-handling
- #include "assert_mac.h"
-
- // ---------------------------------------------------------------------------
-
- void SetupPICSFileSplit(FSSpec *picsFile) {
- FSSpec file;
-
- if (picsFile == NULL) {
- // If we're passed NULL, do a StandardGetFile...
- if (!OpenPICSFile(&file)) return;
- }
- else {
- // Else we already have a file ready for us...
- file = *picsFile;
- }
-
- FInfo fileInfo;
- OSErr myErr;
- myErr = FSpGetFInfo(&file, &fileInfo);
- if (myErr == noErr && fileInfo.fdType == kPICTFileType) {
- SimpleError(kAlertErrID, kErrMsgID, kDontAcceptPICTFileErrMsg);
- return;
- }
-
- // Always save split PICT files with the Photoshop creator type
- SplitPICSFile(kPhotoShopCreatorType, &file, -1);
- } // END SetupPICSFileSplit
-
- // ---------------------------------------------------------------------------
-
- void SplitPICSFile(OSType creatorType, FSSpec *picsFile, short fileRefNum) {
- short oldRefNum;
- Boolean isOpenedAlready;
- ProgressWindowPtr progressWindow;
-
- // We'll still need to use the <picsFile> argument, even if
- // we were passed a valid <fileRefNum>
- ASSERT(picsFile != NULL);
-
- oldRefNum = CurResFile();
-
- if (fileRefNum == -1) {
- ASSERT(picsFile != NULL);
- fileRefNum = FSpOpenResFile(picsFile, fsRdWrPerm);
- if (!RegisterFile(fileRefNum)) {
- UseResFile(oldRefNum);
- SimpleError(kAlertErrID, kErrMsgID, kCantOpenFileErrMsg);
- return;
- }
- isOpenedAlready = false;
- }
- else {
- isOpenedAlready = true;
- }
-
- if (fileRefNum != -1) {
- short numPicts;
- PicHandle picHandle;
- Str15 indexStr;
- Str15 suffixStr = kSplitSuffix;
- Str63 fileName;
-
- UseResFile(fileRefNum);
-
- numPicts = Count1Resources(kPICSRsrcType);
- if (numPicts == 0) {
- UseResFile(oldRefNum);
- SimpleError(kAlertErrID, kErrMsgID, kNoPICTsInFileErrMsg);
- if (!isOpenedAlready) {
- UseResFile(oldRefNum);
- CloseResFile(fileRefNum);
- }
- return;
- }
- else {
- UseResFile(oldRefNum);
- NumToString(numPicts, indexStr);
- ParamText("\pThe number of frames in this PICS file:",
- indexStr, NULL, NULL);
-
- // Ask user to proceed or not. If cancel, exit...
- if (Alert(kAlertMsgID, NULL) == cancel) {
- if (!isOpenedAlready) {
- UseResFile(oldRefNum);
- CloseResFile(fileRefNum);
- }
- UseResFile(oldRefNum);
- return;
- }
- }
-
- UseResFile(fileRefNum);
- AppEmergencyUpdate();
-
- // Assumes id starts at 128, and all PICTs are sequentially id
- for (short i = 1, resID = kPICSRsrcStartID; i <= numPicts; i++, resID++) {
- SetCursor(*GetCursor(watchCursor));
-
- picHandle = (PicHandle)Get1Resource(kPICSRsrcType, resID);
- if (picHandle == NULL) {
- if (i > 1)
- delete progressWindow;
- UseResFile(oldRefNum);
- SimpleError(kAlertErrID, kErrMsgID, kUnableLoadPICTErrMsg);
- if (!isOpenedAlready) {
- CloseResFile(fileRefNum);
- }
- UseResFile(oldRefNum);
- return;
- }
-
- DetachResource((Handle)picHandle);
-
- /*
- Here we make it convenient for the user (especially if there's
- a lot of frames). For the first exported frame, we ask the
- user for the location to save it, via StandardPutFile. From
- this file we get the necessary FSSpec data.
- Then on we save all the files by making the necessary dest
- FSSpec, thus removing the necessity for asking the user a
- StandardPutFile for every exported frame...
- */
- StandardFileReply reply;
- OSErr osErr;
- Str31 baseFileName;
- if (i == 1) {
- // -------------------------------------
- // ADJUST FILENAME & APPEND FRAME NUMBER
- // -------------------------------------
- PStrCpy(picsFile->name, fileName);
- // Make sure filename is long enough to hold our suffixes;
- // else crop it to make it fit...
- // Here we give it enough for one char (•) and three digits
- if (fileName[0] > 27)
- fileName[0] = 27;
- PStrCat(suffixStr, fileName);
- NumToString(i, indexStr);
- PStrCat(indexStr, fileName);
-
- // ----------------------------------
- // GET FIRST EXPORTED FRAME LOCATION
- // ----------------------------------
- SetCursor(&qd.arrow);
- StandardPutFile("\pSave first PICT file into:", fileName, &reply);
- if (!reply.sfGood) {
- if (!isOpenedAlready) {
- DisposeHandle((Handle)picHandle);
- CloseResFile(fileRefNum);
- }
- UseResFile(oldRefNum);
- return;
- }
- SetCursor(*GetCursor(watchCursor));
-
- AppEmergencyUpdate();
-
- /*
- Another "smart" feature... We supply the user with the
- filename suffixed by "•1". If the user should change
- the name, we'll use that instead for all the exported
- PICTs.
- */
- PStrCpy(reply.sfFile.name, baseFileName);
- if (baseFileName[baseFileName[0]] == '1' &&
- baseFileName[baseFileName[0]-1] == suffixStr[1]) {
- baseFileName[0] = baseFileName[0] - 2;
- }
- if (baseFileName[0] > 27)
- baseFileName[0] = 27;
-
- // Show progress window
- progressWindow = new ProgressWindow(GetDeepestDevice(), numPicts);
- SetPort(progressWindow->GetWindow());
- TextFont(geneva);
- TextSize(9);
- TextFace(bold);
- }
-
- progressWindow->SetPrimaryMessage("\pSplitting PICS file...");
- progressWindow->Increment();
-
- PStrCpy(baseFileName, fileName);
- PStrCat(suffixStr, fileName);
- NumToString(i, indexStr);
- PStrCat(indexStr, fileName);
-
-
- // ----------------------------------
- // MAKE NEW DESTINATION FSSPEC
- // ----------------------------------
- FSSpec destFile;
- osErr = FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID,
- fileName, &destFile);
- // Note: FSMakeFSSpec will return an fnfErr error code, but
- // the FSSpec is still valid.
- if (osErr != noErr && osErr != fnfErr) {
- delete progressWindow;
- if (!isOpenedAlready) {
- DisposeHandle((Handle)picHandle);
- CloseResFile(fileRefNum);
- }
- UseResFile(oldRefNum);
- SimpleError(kAlertErrID, kErrMsgID, kCantMakeFSSpecErrMsg);
- return;
- }
-
- // --------------------------------------
- // IF FILE EXISTS, UNILATERALLY DELETE IT
- // --------------------------------------
- short refNum;
- osErr = FSpOpenDF(&destFile, fsCurPerm, &refNum);
- if (osErr == noErr) {
- // We were able to open the file, so means that
- // it exists (duh!) Close it and delete it...
-
- progressWindow->SetPrimaryMessage("\pDeleting previous file...");
- progressWindow->SetSecondaryMessage(destFile.name);
-
- FSClose(refNum);
- osErr = FSpDelete(&destFile);
- if (osErr != noErr) {
- delete progressWindow;
- if (!isOpenedAlready) {
- DisposeHandle((Handle)picHandle);
- CloseResFile(fileRefNum);
- }
- UseResFile(oldRefNum);
- SimpleError(kAlertErrID, kErrMsgID, kCantDeleteFileErrMsg);
- return;
- }
- }
-
- // ----------------------------------
- // CREATE PICT FILE
- // ----------------------------------
- osErr = FSpCreate(&destFile, creatorType,
- kPICTFileType, reply.sfScript);
- progressWindow->SetPrimaryMessage("\pSaving PICT file...");
- progressWindow->SetSecondaryMessage(destFile.name);
-
- if (osErr == noErr && !SavePictureFile(&destFile, ((Handle)picHandle))) {
- delete progressWindow;
- if (!isOpenedAlready) {
- DisposeHandle((Handle)picHandle);
- CloseResFile(fileRefNum);
- }
- UseResFile(oldRefNum);
- SimpleError(kAlertErrID, kErrMsgID, kCantCreateFileErrMsg);
- return;
- }
-
- DisposeHandle((Handle)picHandle);
- }
-
- delete progressWindow;
- FlushEvents(everyEvent, 0); // In case mouse was clicked during progress window
-
- if (!isOpenedAlready) {
- UseResFile(oldRefNum);
- CloseResFile(fileRefNum);
- }
- }
- else {
- SimpleError(kAlertErrID, kErrMsgID, kCantOpenFileErrMsg);
- }
-
- UseResFile(oldRefNum);
- } // END SplitPICSFile